//
//  AgoraRTMClient.swift
//  LivelyChat
//
//  Created by admin on 2021/8/2.
//

import UIKit
import AgoraRtmKit

class AgoraRTMClient: NSObject,PixelWorldChatProtocol{

    // 126413953d0447a698c4bca7086d970f
    static let shared = AgoraRTMClient()
    var _kit = AgoraRtmKit(appId: "126413953d0447a698c4bca7086d970f", delegate: nil)
    
    func login() -> Void {
        self._kit?.login(byToken: nil, user: USEINFOMODEL.id!, completion: { (code) in
            if code == .ok
            {
                print("login==-------  ")
                print(USEINFOMODEL.id!)
            }
        })
    }
    
    // 发送消息
    func sendMessage(msg:AgoraRtmMessage,userIdString:String,comp:@escaping ((Int) -> Void)) {
        let option =  AgoraRtmSendMessageOptions()
//        option.enableOfflineMessaging = true
        self._kit?.send(msg, toPeer: userIdString, sendMessageOptions: option, completion: { code in
            if code.rawValue == 102 {
                AgoraRTMClient.shared.login()
            }
            print(code.rawValue)
            print("sendMessage===-----  ")
            let codeValue = code.rawValue
            comp(codeValue)
        })
    }
    
    func sendHyMessage(_ type: PixelWorldChatMessageBodyType,img:UIImage?, content: String,userid:String,conversationID:String,resultBlock: PixelWorldChatStatusBlock? = nil) {
        var body = PixelWorldChatSendMessageBody()
        body.message = content
        body.image = img
        body.conversationID = conversationID
        body.messageBody = type
        body.sender_identifies = USEINFOMODEL.id!
        body.receive_identifies = userid
        chat.sendMessage(body, resultBlock: resultBlock)
    }

}
